package com.ruoyi.web.controller.tool;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.core.config.DefaultSchedulingConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.config.TriggerTask;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author XuYanK
 * @date 2021/4/25 16:21
 */

@Slf4j
@RestController
@RequestMapping("DefaultScheduling")
@Api(value = "默认定时任务API", tags = {"默认定时任务管理"})
public class DefaultSchedulingController {

    @Resource
    private DefaultSchedulingConfig defaultSchedulingConfig;

    public static String CRON = "0/5 * * * * ? ";

    @GetMapping("/hasTask")
    @ApiOperation("查询同一个线程池中任务是否存在")
    public AjaxResult hasTask(@RequestParam String taskName) {
        return AjaxResult.success(defaultSchedulingConfig.hasTask(taskName));
    }


    @GetMapping("/addTaskIsOne")
    @ApiOperation("添加一次性任务")
    public AjaxResult addTaskIsOne(@RequestParam String taskName) {
        defaultSchedulingConfig.addTaskIsOne(taskName, new Date(),
                new TriggerTask(() -> {
                    //业务代码
                },
                new CronTrigger(CRON)));
        return AjaxResult.success();
    }
}
